package com.uam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.uam.common.exception.CustomException;
import com.uam.db.mapper.MenuMapper;
import com.uam.db.mapper.RoleMapper;
import com.uam.db.mapper.RoleMenuMapper;
import com.uam.db.model.entity.Menu;
import com.uam.db.model.entity.Role;
import com.uam.db.model.entity.RoleMenu;
import com.uam.service.RoleService;
import com.uam.utils.StringUtils;
import com.uam.web.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author uam
 * @create 2023/11/30 20:23
 * @email uam@uam.com
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override

    public Result getRole() {
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.select(Role::getRoleId, Role::getRoleName);
        List<Role> roles = roleMapper.selectList(roleLambdaQueryWrapper);
        return Result.createSuccess(roles);
    }

    @Override
    public Result getMenuByRole(String roleName) {
        Long roleId = null;
        List<Long> meniIds = null;
        if (StringUtils.isNotEmpty(roleName)) {
            roleId = getRoleIdByRoleName(roleName);
            LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roleMenuLambdaQueryWrapper.select(RoleMenu::getMenuId);
            roleMenuLambdaQueryWrapper.eq(roleId != null, RoleMenu::getRoleId, roleId);
            List<RoleMenu> roleMenuList = roleMenuMapper.selectList(roleMenuLambdaQueryWrapper);
            meniIds = roleMenuList.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        }

        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.select(Menu::getMenuId, Menu::getFirstName, Menu::getSecondName, Menu::getMenuName, Menu::getUrl);
        menuLambdaQueryWrapper.in(CollectionUtils.isNotEmpty(meniIds), Menu::getMenuId, meniIds);
        List<Menu> menus = menuMapper.selectList(menuLambdaQueryWrapper);
        return Result.createSuccess(menus);
    }

    @Override
    public Long getRoleIdByRoleName(String roleName) {
        LambdaQueryWrapper<Role> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(StringUtils.isNotEmpty(roleName), Role::getRoleName, roleName);
        Role role = roleMapper.selectOne(userQueryWrapper);
        if (null == role) {
            throw new CustomException("角色不存在");
        }
        return role.getRoleId();

    }

    @Override
    public Result setRoleMenu(String roleName, List<Long> menuIds) {
        Long roleid = getRoleIdByRoleName(roleName);
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId, roleid);
        roleMenuMapper.delete(roleMenuLambdaQueryWrapper);
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.select(Menu::getMenuId);
        menuLambdaQueryWrapper.in(CollectionUtils.isNotEmpty(menuIds), Menu::getMenuId, menuIds);
        List<Menu> menus = menuMapper.selectList(menuLambdaQueryWrapper);
        ArrayList<Long> res = new ArrayList<>();
        menus.forEach(menu -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleid);
            roleMenu.setMenuId(menu.getMenuId());
            roleMenuMapper.insert(roleMenu);
            res.add(menu.getMenuId());
        });
        return Result.createSuccess("配置成功",res);
    }

    @Override
    public Result addRole(String roleName) {
        Role role = new Role();
        role.setRoleName(roleName);
        roleMapper.insert(role);
        return Result.createSuccess("增加角色成功");
    }
}
